import { DataTypes, Model } from 'sequelize'
import { sequelize } from './index'

export class Transaction extends Model {
  public id!: string
  public userId!: number
  public type!: string
  public amount!: number
  public balance!: number
  public status!: string
  public remark?: string
}

Transaction.init(
  {
    id: {
      type: DataTypes.STRING(32),
      primaryKey: true,
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    type: {
      type: DataTypes.ENUM(
        'bet', // 下注
        'win', // 赢钱
        'recharge', // 充值
        'withdraw', // 提现
        'gift', // 礼物
        'commission' // 佣金
      ),
      allowNull: false,
    },
    amount: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    balance: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    status: {
      type: DataTypes.ENUM('pending', 'success', 'failed'),
      defaultValue: 'pending',
    },
    remark: {
      type: DataTypes.STRING(200),
    },
  },
  {
    sequelize,
    tableName: 'transactions',
    indexes: [
      {
        fields: ['userId'],
      },
      {
        fields: ['type'],
      },
      {
        fields: ['createdAt'],
      },
    ],
  }
)
